﻿__author__ = 'zhenjiao.su'
# -*- coding: utf-8 -*-
from time import sleep
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from Newwebdriver import Active
def splittext(text,loc,str):  #自定义切分
    return (text.split(str))[loc]
class pageother():
    def __init__(self,driver):
        self.driver=driver
    def pageoverexit(self):
        try:
            self.driver.find_element_by_css_selector(".cancel js_p_s_btn")
            return True
        except:
            return False
    def pageoverclick(self):
        self.driver.find_element_by_css_selector(".cancel.js_p_s_btn").click()
class login(Active):     #登陆弹出框的类
    def __init__(self,driver):
        Active.__init__(self,driver)
        self.driver=driver
        self.username=("name","username")
        self.password=("name","password")
        self.loginbutton=("css selector",".login_submit>button")
        self.loginstatu=("name","q_header_uname")
    def logins(self):
        self.find_element(self.username).clear()
        self.find_element(self.username).send_keys("13603068412")
        self.find_element(self.password).clear()
        self.find_element(self.password).send_keys("520041")
        self.find_element(self.loginbutton).click()
        sleep(4)
        try:      #判断登陆成功后，顶部是否有对应的登陆名
            WebDriverWait(self.driver,10).until(lambda a:a.find_elemen(*(self.loginstatu)))
            print(u"登陆成功")
        except:
            print(u"登陆异常")
            raise SystemError
def loading(driver,timeout=30):
#判断loading状态，若元素不存在则True,元素存在，但是隐藏则False，即存在，也显示,超出30S，则"warning"
    sleep(3)
    for i in range(3,3000):
        print(u".......已载入%s秒........."%i)
        if i==timeout:
            return "warning"
        try:
            driver
        except:
            return True
        else:
            loadingdis=driver.is_displayed()
            if loadingdis==False:
                return False
        sleep(1)
def datetimes(times,date):     #指定要使用的日期times自定义日期，date自定义要去的该日期+天数
    timeArray=time.strptime(times,"%Y-%m-%d")   #转为时间数组
    timestamp=time.mktime(timeArray)       #将获得的时间数组转为时间戳
    timestamps=timestamp+(date*86400)     #获得时间戳加上自定义天数的总秒数获得，新日期的时间戳
    timeArrays=time.localtime(timestamps)  #将新日期的时间戳转为时间数组
    return time.strftime("%Y-%m-%d",timeArrays)    #将时间数组按照日期格式输出
def browser(driver,handle):
    handles=driver.window_handles
    for handle2 in handles:
        if handle2 !=handle:
            driver.close()
            driver.switch_to_window(handle2)





